package com.dadaguai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dadaguai.domain.ForumBoard;
import com.dadaguai.service.ForumBoardService;
import com.dadaguai.mapper.ForumBoardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 王半仙
* @description 针对表【forum_board(文章板块信息)】的数据库操作Service实现
* @createDate 2023-10-20 11:28:40
*/
@Service
public class ForumBoardServiceImpl extends ServiceImpl<ForumBoardMapper, ForumBoard>
    implements ForumBoardService{

    @Autowired
    ForumBoardMapper boardMapper;

    @Override
    public List<ForumBoard> getAllBoard() {
        LambdaQueryWrapper<ForumBoard> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.orderByAsc(ForumBoard::getSort);
        List<ForumBoard> boardList = boardMapper.selectList(queryWrapper);

        return getTreeList(boardList,"0");
    }

    /**
     * 组装树形数据
     * @param dataList
     * @param id
     * @return
     */
    private List<ForumBoard> getTreeList(List<ForumBoard> dataList, String id) {
        List<ForumBoard> childList = new ArrayList<>();
        for (ForumBoard board : dataList) {
            if (board.getPBoardId().equals(id)) {
                board.setChildren(getTreeList(dataList, board.getBoardId()));
                childList.add(board);
            }
        }
        return childList;
    }
}




